package com.kevin.spring.dto.event;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImp implements StudentService,
		ApplicationContextAware {

	private ApplicationContext applicationContext;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.kevin.spring.dto.event.StudentService#register(com.kevin.spring.dto
	 * .event.Course, com.kevin.spring.dto.event.Student)
	 */
	public void register(Course course, Student student) {

		if (course.getCapacity() < Course.MAX_CAPACITY) {
			course.addNewStudent();
			student.setStudentId(course.getCapacity());
			System.out.println("恭喜您，注册成功");

			RegisteredEvent registeredEvent = new RegisteredEvent(this);
			registeredEvent.setStudenId(course.getCapacity());
			this.applicationContext.publishEvent(registeredEvent);
		} else {

			System.out.println("该课程已经人满，注册失败");
			this.applicationContext.publishEvent(new CourseFullEvent(this));
		}

	}

	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {

		this.applicationContext = applicationContext;

	}
}
